package yunjiao.javatutorials.guava.concurrent.timed;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;

/**
 * 生产环境配置
 *
 * @author yangyunjiao
 */
@Slf4j
public class ProductionTimedCondition extends TimedCondition {
    public ProductionTimedCondition(Condition delegate) {
        super(delegate);
    }

    @Override
    public boolean await(long time, TimeUnit unit) throws InterruptedException {
        long startTime = System.nanoTime();
        boolean result = super.await(time, unit);
        long duration = System.nanoTime() - startTime;

        // 生产环境日志
        if (log.isDebugEnabled()) {
            log.debug("Condition await completed: duration={}ns, result={}, timeout={}",
                    duration, result, !result);
        }

        // 监控超时率
        if (!result) {
            log.warn("Condition await timed out after {} {}", time, unit);
        }

        return result;
    }
}
